
/*
 * Copyright (C) 2010 Bitglue. All rights reserved.
 *
 * $Id: AdminUser.java bself $
 */

package com.bitglue.hibernate.model;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;

/**
 * AdminUser models a patchwork server admin user.
 * @author Bryan Patrick Self
 */
@Entity
public class AdminUser extends BaseEntity {

    private String realname;
    private String username;
    private String passwordHash;
    private String email;
    private String title;
    private Date timeLastLogin;
    private String department;

    @Column(nullable = false, length = 48)
    public String getRealname() {
        return realname;
    }
    public void setRealname(String realname) {
        this.realname = realname;
    }

    @Column(nullable = false, unique = true, length = 32)
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

    @Column (length = 32)
    public String getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }

    @Column(length = 128)
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

    @Column(name = "password", nullable = false, length = 64)
    public String getPasswordHash() {
        return passwordHash;
    }
    public void setPasswordHash(String passwordHash) {
        this.passwordHash = passwordHash;
    }

    @Column(length = 32)
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

    public Date getTimeLastLogin() {
        return timeLastLogin;
    }
    public void setTimeLastLogin(Date timeLastLogin) {
        this.timeLastLogin = timeLastLogin;
    }

    /**
     * md5hash() returns md5 digest of input string.
     */
    public static String md5hash(String str) throws Exception {
        MessageDigest m = MessageDigest.getInstance("MD5");
        byte[] data = str.getBytes();
        m.update(data, 0, data.length);
        BigInteger i = new BigInteger(1, m.digest());
        return String.format("%1$032X", i);
    }

}
